<template>
  <div>
    <el-form :inline="true" :model="page" style="margin-top: 20px;">
      <el-form-item label="博客标题">
        <el-input v-model="page.params.blogTitle" clearable placeholder="博客标题" />
      </el-form-item>
      <el-form-item label="用户昵称">
        <el-input v-model="page.params.nickname" clearable placeholder="用户昵称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="page.list"
      border
      style="width: 100%"
      element-loading-text="数据加载中"
      element-loading-spinner="el-icon-loading"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="评价内容">
              <span>{{ props.row.commentContent }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column
        prop="id"
        label="评论ID"
      />
      <el-table-column
        prop="commentUser"
        label="评价人ID"
      />
      <el-table-column
        prop="user.nickname"
        label="评价人昵称"
      />
      <el-table-column
        prop="blogVo.blogTitle"
        label="博客标题"
      />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handlerDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.countTotal"
      class="pagination-bottom"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import commentApi from '@/api/comment'
export default {
  name: 'BlogList',
  data() {
    return {
      page: {

      },
      loading: false
    }
  },
  created() {
    this.getCommentList()
  },
  methods: {
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getCommentList()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getCommentList()
    },
    getCommentList() {
      this.loading = true
      commentApi.getByPage(this.page).then(res => {
        if (res.code === 200) {
          this.page = res.data
          this.loading = false
        } else {
          this.loading = false
        }
      }).catch(() => {
        this.loading = false
      })
    },
    onSubmit() {
      this.getCommentList()
    },
    handlerDelete(id) {
      this.$confirm('此操作将删除该评论, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        commentApi.delete(id).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.getCommentList()
          } else {
            this.$message.error(res.msg)
          }
        })
      })
    }
  }

}
</script>

<style scoped>

</style>
